﻿using IM.Easy.Core.OAuth.Providers;
using IM.Easy.Core.OAuth.ProvidersApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Refit;
using System;
using System.Collections.Generic;
using System.Configuration.Provider;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.OAuth
{
    public class OAuthProviderBuilder
    {
        private readonly IServiceCollection _services;
        private readonly Dictionary<string, Type> _providers = [];
        private readonly IServiceCollection _innerServiceCollection;

        public OAuthProviderBuilder(IServiceCollection services)
        {
            _services = services;
            _innerServiceCollection = new ServiceCollection();
        }

        public OAuthProviderBuilder AddProvider<TProvider>(string name, Action<AuthOptions> configure)
            where TProvider : AuthProviderAbstract
        {
            // 使用工厂方法注册提供程序
            _innerServiceCollection.AddTransient(serviceProvider =>
            {
                // 调用配置方法获取 AuthOptions 实例
                var options = new AuthOptions();
                configure(options);

                // 确保实例化正确
                return ActivatorUtilities.CreateInstance<TProvider>(serviceProvider, options);
            });
            // 注册提供程序类型到工厂
            RegisterProvider<TProvider>(name);

            return this;
        }

        public OAuthProviderBuilder AddProvider<TProvider>(string name)
         where TProvider : AuthProviderAbstract
        {
            // 注册提供程序类型到工厂
            RegisterProvider<TProvider>(name);
            return this;
        }


        /// <summary>
        /// 注册提供程序类型。
        /// </summary>
        private void RegisterProvider<TProvider>(string name) where TProvider : AuthProviderAbstract
        {
            _providers[name] = typeof(TProvider);
        }

        public OAuthProviderFactory Build()
        {
            //_services.AddSingleton(provider =>
            //{
            //    var serviceProvider = _innerServiceCollection.BuildServiceProvider();
            //    return new OAuthProviderFactory(serviceProvider, new Dictionary<string, Type>());
            //});

            var factory = new OAuthProviderFactory(_innerServiceCollection.BuildServiceProvider(), _providers);
            _services.AddSingleton(factory);
            return factory;
        }

    }



    public static class OAuthProviderBuilderExtension
    {

        public static OAuthProviderBuilder AddGiteeProvider(this OAuthProviderBuilder builder, Action<AuthOptions> configure)
        {

            builder.AddProvider<GiteeOAuthProvider>(OAuthProviders.Gitee, configure);
            return builder;
        }

        /// <summary>
        /// 需要在创建实例的时候进行配置
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static OAuthProviderBuilder AddGiteeProvider(this OAuthProviderBuilder builder)
        {

            builder.AddProvider<GiteeOAuthProvider>(OAuthProviders.Gitee);
            return builder;
        }
    }



}
